package cn.yanxiaohui.common.hander;

import cn.yanxiaohui.common.util.constant.enums.BookMarkType;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 枚举和持久层数据的映射
 */
@MappedTypes({BookMarkType.class})
public class EnumTypeHander implements TypeHandler<BookMarkType> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, BookMarkType bookMarkType, JdbcType jdbcType) throws SQLException {
        preparedStatement.setInt(i, bookMarkType.getValue());
    }

    @Override
    public BookMarkType getResult(ResultSet resultSet, String s) throws SQLException {
        Integer value = resultSet.getInt(s);
        return BookMarkType.getMessageByValue(value);
    }

    @Override
    public BookMarkType getResult(ResultSet resultSet, int i) throws SQLException {
        Integer value = resultSet.getInt(i);
        return BookMarkType.getMessageByValue(value);
    }

    @Override
    public BookMarkType getResult(CallableStatement callableStatement, int i) throws SQLException {
        Integer value = callableStatement.getInt(i);
        return BookMarkType.getMessageByValue(value);
    }

}